-- UIMyCardPay
-- myCard支付页面
-- create by huanghr

UIMyCardPay = class("UIMyCardPay", function()
    return cc.Layer:create();
end);
    
-- 固定金额数量
local FIXED_AMOUNT = 6;

-- pt商品编号
local PT_GOODS_ID = 10101;

function UIMyCardPay.create()
    return UIMyCardPay.new();
end

-- 构造函数
function UIMyCardPay:ctor()
    self:setName("UIMyCardPay");

    -- 初始化
    local node = cc.CSLoader:createNode("layout/market/MyCardPay.csb");
    self:addChild(node);
    self.node = node;
    -- 获取固定的充值金额
    local payAmount = FormulaM.invoke("GET_MYCARD_PAY_AMOUNT") or {};
    self.payAmount = payAmount;

    if DungeonM.isInDungeon() then
        -- 迷宫中层级要调高
        self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 11);
    end

    -- 注册事件处理回调函数
    self:registerEventCallback()

    -- 注册各种点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 注册点击事件
function UIMyCardPay:registerTouchEvent()
    local node = self.node;
    local CT = findChildByName(self.node, "CT");

    -- 注册背景点击事件(点击背景，取消当前)
    local bgImg = findChildByName(self.node, "bg/bgImg");
    local function onBgClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIMyCardPay");
        end
    end
    bgImg:addTouchEventListener(onBgClick);

    -- 添加固定金额按钮监听
    for i = 1, FIXED_AMOUNT do  
        local payBtn = findChildByName(CT, "btn_" .. i);
        payBtn.index = i;
        local function onPayClick(sender, eventType)
            if eventType == ccui.TouchEventType.ended then 
                -- 获得当前充值金额
                local index = sender.index;
                local amount = self.payAmount[index] or 0;
                -- 充值金额不能为0
                if amount <= 0 then
                    return; 
                end
                self:tryRecharge(amount);
            end

        end

        payBtn:addTouchEventListener(onPayClick);
        -- TextStyleM.setTextStyle(payBtn, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_WHITE);
        payBtn:setTitleFontSize(TextStyleM.TEXT_SIZE_MIDDLE);
        payBtn:setTitleColor(TextStyleM.TEXT_COLOR_WHITE);

        local payStr = getLocStr("apk_currency_nt");
        payStr = string.format(payStr, self.payAmount[i]);

        payBtn:setTitleText(payStr);
    end

    -- 输入其他的金额
    local otherPayBtn = findChildByName(CT, "btn_7");
    local function otherPayClick(sender, eventType)
        -- 打开新输入框，输入对应的充值数
        if eventType == ccui.TouchEventType.ended then
            -- 打开输入输入框页面
            require "game/ui/form/market/UIPayAmountInput";
            UIMgr.getCurrentScene():removeFormByName("UIPayAmountInput");
            local uiForm = UIPayAmountInput.create();
            UIMgr.getCurrentScene():addForm(uiForm);
        end
    end
    otherPayBtn:addTouchEventListener(otherPayClick);

    otherPayBtn:setTitleFontSize(TextStyleM.TEXT_SIZE_MIDDLE);
    otherPayBtn:setTitleColor(TextStyleM.TEXT_COLOR_WHITE);
    local otherPayStr = getLocStr("apk_other_amount");
    otherPayBtn:setTitleText(otherPayStr);
end

-- 注册事件处理回调函数
function UIMyCardPay:registerEventCallback()
    self:registerScriptHandler(function(ev)
        if ev == "exit" then
            cclog("UIMyCardPay 界面析构清理");
            EventMgr.removeAll("UIMyCardPay");
        end
    end);
end

-- 重绘
function UIMyCardPay:redraw()
    local CT = findChildByName(self.node, "CT");

    local titleLabel = findChildByName(CT, "title_label");

    -- TextStyleM.setTextStyle(titleLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_WHITE);
    titleLabel:setFontSize(TextStyleM.TEXT_SIZE_MIDDLE);
    titleLabel:setColor(TextStyleM.TEXT_COLOR_WHITE);
    
    local titleStr = getLocStr("apk_recharge_amount");
    titleLabel:setString(titleStr);
end

-- 适配
function UIMyCardPay:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");
end

-- 充值
function UIMyCardPay:tryRecharge(amount)
    MarketM.buyByRmb(PT_GOODS_ID, "mycard", amount);
end